package com.sjz.rydemo.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.sjz.rydemo.XYApplication;


/**
 * @author xiejiachun
 * @date 2022/7/19.
 * description： 万能Share
 */

public class XYSPUtils {
    /**
     * 万能的put方法(可以存储String,int,boolean)
     *
     * @param key
     * @param value
     */
    public static void put(String key, Object value) {
        SharedPreferences sp = XYApplication.getAppApplicationContext().getSharedPreferences("config", Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sp.edit();
        if (value instanceof String) {
            edit.putString(key, (String) value);
        } else if (value instanceof Integer) {
            edit.putInt(key, (int) value);
        } else if (value instanceof Boolean) {
            edit.putBoolean(key, (boolean) value);
        }
        edit.commit();
    }

    /**
     * 获取字符串
     *
     * @param key
     * @return
     */
    public static String getString(String key) {
        SharedPreferences sp = XYApplication.getAppApplicationContext().getSharedPreferences("config", Context.MODE_PRIVATE);
        return sp.getString(key, "");
    }

    /**
     * 获取整数
     *
     * @param key
     * @return
     */
    public static int getInt(String key) {
        SharedPreferences sp = XYApplication.getAppApplicationContext().getSharedPreferences("config", Context.MODE_PRIVATE);
        return sp.getInt(key, 0);
    }

    /**
     * 获取布尔
     *
     * @param key
     * @return
     */
    public static boolean getBoolean(String key) {
        SharedPreferences sp = XYApplication.getAppApplicationContext().getSharedPreferences("config", Context.MODE_PRIVATE);
        return sp.getBoolean(key, false);
    }

    /**
     * 删除
     *
     * @param key
     * @return
     */
    public static void delete(String key) {
        SharedPreferences sp = XYApplication.getAppApplicationContext().getSharedPreferences("config", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.remove(key);
        editor.commit();

    }

    /**
     * 清空
     *
     * @param context
     * @return
     */
    public static void clear(Context context) {
        SharedPreferences sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.clear();
        editor.commit();

    }
}
